package com.nandi.deviceinstall.presenter

import android.annotation.SuppressLint
import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.*
import com.nandi.deviceinstall.contract.EquimentStepContract
import com.nandi.deviceinstall.contract.LoginContract
import com.nandi.deviceinstall.contract.TapRecordContract
import com.nandi.deviceinstall.model.EquipmentStepModel
import com.nandi.deviceinstall.model.LoginModel
import com.nandi.deviceinstall.model.TapRecordModel
import com.nandi.deviceinstall.net.APIService
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RetrofitClient
import com.nandi.deviceinstall.net.RxScheduler
import com.nandi.deviceinstall.net.dto.TapAddDTO
import com.uber.autodispose.FlowableSubscribeProxy
import io.reactivex.Flowable
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.reactivestreams.Subscription
import java.io.File


class EquipmentStepPresenter : BasePresenter<EquimentStepContract.View>(), EquimentStepContract.Presenter {

    override fun requestDisName(token: String, disasterName: String, town: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeListDisName(token, disasterName,town)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNameBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNameBean>(mView!!) {
                    override fun onNext(t: DisNameBean) {
                        mView!!.handlerDisNameResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    private val apiService = RetrofitClient.getInstance()!!.api!!

    private val model: EquimentStepContract.Model

    init {
        model = EquipmentStepModel()
    }

    override fun requestUpdateTap(token: String, params: TapAddDTO) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeUpdateTap(token,params)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TapAddResultBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TapAddResultBean>(mView!!) {
                    override fun onNext(t: TapAddResultBean) {
                        mView!!.handlerUpdateTapResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestAddTap(token: String, params: TapAddDTO) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeAddTap(token, params)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TapAddResultBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TapAddResultBean>(mView!!) {
                    override fun onNext(t: TapAddResultBean) {
                        mView!!.handlerAddTapResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestDisNum(token: String, disName: String, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDisNum(token,disName,areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNumBean>(mView!!) {
                    override fun onNext(t: DisNumBean) {
                        mView!!.handlerDisNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListDisaster(token: String, current: Int, size: Int, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListDisaster(token,areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisasterBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisasterBean>(mView!!) {
                    override fun onNext(t: DisasterBean) {
                        mView!!.handlerListDisasterResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListTown(token: String, id: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListTown(token, id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TownBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TownBean>(mView!!) {
                    override fun onNext(t: TownBean) {
                        mView!!.handlerListTownResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
    
    override fun requestListCity(token: String, district: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListCity(token, district)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<CityBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<CityBean>(mView!!) {
                    override fun onNext(t: CityBean) {
                        mView!!.handlerListCityResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    @SuppressLint("CheckResult")
    override fun requestUploadPic(token: String, file: File, type: Int) {
        var fileName = ""
        var resultPath = ""
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.uploadPic(token, file, type)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<UploadPicBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<UploadPicBean>(mView!!) {
                    override fun onNext(t: UploadPicBean) {
                        mView!!.uploadPicSuccess(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
                /*.subscribeOn(Schedulers.io())
                .flatMap { t ->
                    fileName = t.data.substring(t.data.lastIndexOf("/") + 1)
                    resultPath = t.data
                    apiService.downPic(token,t.data,6)
                }
                .observeOn(AndroidSchedulers.mainThread())
                .`as`<FlowableSubscribeProxy<ResponseBody>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ResponseBody>(mView!!) {
                    override fun onNext(t: ResponseBody) {
                        //mView!!.uploadPicSuccess(t)
                        mView!!.downPicSuccess(t,fileName,resultPath)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })*/
    }

    override fun requestRemovePicFile(token: String, sourcePath: String, type: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.removePicFile(token, sourcePath, type)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<RemovePicBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<RemovePicBean>(mView!!) {
                    override fun onNext(t: RemovePicBean) {
                        mView!!.removePicFile(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}

